var neo4j = require('neo4j-driver').v1;

var driver = neo4j.driver("bolt://localhost:7687", neo4j.auth.basic("neo4j", "pgmopen"));

const session = driver.session();

//使用session直接运行cypher语句
function addPerson(personName) {
    return session.run('CREATE (a:Person {name: $name})', {
            'name': personName
        }).then(result =>{
            session.close(() =>{
            console.log('Person created, session closed');
});
    return result;
});
}
//使用writeTransaction创建“写事务”并执行cypher语句，然后返回结果
function writeTransactionDemo(personName) {
    const writeTxPromise = session.writeTransaction(tx =>tx.run('CREATE (a:Person {name: $name})', {
            'name': personName
        }));
    writeTxPromise.then(result =>{
        session.close();
    if (result) {
        console.log('Person created');
    }
});
}


exports.findAll = function (callback) {
    fs.readFile(dbpath, function (err, data) {
        if(err){
            return callback(err)
        }
        callback(null,JSON.parse(data.toString()).students)
    })
}


//使用readTransaction创建“读事务”，运行cypher语句并返回结果
function readTransactionDemo(personName) {
    const readTxPromise = session.readTransaction(tx =>tx.run('MATCH (a:Person {name: $name}) return a', {
            'name': personName
        }));
    readTxPromise.then(result =>{
        session.close();
    if (result) {
        console.log(result);
    }
})
}

//addPerson("pangguoming");
//writeTransactionDemo("pangguoming2");
readTransactionDemo("pangguoming");
